<?php

require_once 'Mobitv/Admin/Action.php';

class IndexAction extends Mobitv_Admin_Action {

    public function execute() {
        // Neu chi la CP, khong phai CM hay Admin thi chi duoc xem myvideo
        if (isset($_SESSION['viewall']) && !$_SESSION['viewall'])
            $this->redirect('/myvideo');
        $this->checkPermission();
        $this->updateLoginStatus();
        // Lấy danh sách các chuyên mục, để lọc
        $videoCats = Vega_Dao::factory('Mobitv_Admin_Videocat')->getList(false);
        $this->_view->assign('videoCats', $videoCats);
        // Mã chuyên mục lọc ( 0 ứng với ko lọc)
        $catId = isset($_GET['catId']) ? intval($_GET['catId']) : 0;
        $this->_view->assign('catId', $catId);

        // Lọc video theo yêu cầu liệt kê, mặc định sẽ lấy
        // các video đã được kích hoạt (convert okie và đã qua kiểm duyệt)
        $type = isset($_GET['type']) ? $_GET['type'] : 'active';
        switch ($type) {
            case 'pending':
                $condition = 'converted=1 AND active=0 AND deleted=0';
                break;
            case 'active':
                $condition = 'converted=1 AND active=1 AND deleted=0';
                break;
            case 'deleted':
                $condition = 'deleted=1';
                break;
            case 'notconvert':
                $condition = 'converted=0';
                break;
            case 'convert_fail':
                $condition = 'converted=-1';
                break;
            default:
                $condition = '';
                break;
        }
        $this->_view->assign('type', $type);

        if ($catId)
            $condition = ($condition) ? ($condition . ' AND category_id=' . $catId) : ('category_id=' . $catId);

        $limit = $this->_config['video']['items_per_page'];
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;
        $this->_view->assign('curPage', $page);

        $videoDao = Vega_Dao::factory('Mobitv_Admin_Video');
        $featuredVideos = $videoDao->getIdListFeaturedVideo();
        $this->_view->assign('featuredVideos', $featuredVideos);
        $hotVideo = $videoDao->getIdListHotVideo();
        $this->_view->assign('hotVideo', $hotVideo);
        $weekVideo = $videoDao->getIdListHotVideo(1);
        $this->_view->assign('weekVideo', $weekVideo);
        $videos = $videoDao->getList($limit, $page, 0, $condition);
        $this->_view->assign('videos', $videos);

        switch ($type) {
            case 'pending':
                $condition = 'converted=1 AND active=0 AND c.deleted=0';
                break;
            case 'active':
                $condition = 'converted=1 AND active=1 AND c.deleted=0';
                break;
            case 'deleted':
                $condition = 'c.deleted=1';
                break;
            case 'notconvert':
                $condition = 'converted=0';
                break;
            case 'convert_fail':
                $condition = 'converted=-1';
                break;
            default:
                $condition = '';
                break;
        }
        $this->_view->assign('type', $type);

        if ($catId)
            $condition = ($condition) ? ($condition . ' AND category_id=' . $catId) : ('category_id=' . $catId);

        $totalVideo = $videoDao->countAll(0, $condition);
        $totalPage = ceil($totalVideo / $limit);
        $this->_view->assign('totalVideo', $totalVideo);
        $this->_view->assign('totalPage', $totalPage);
    }

}

